package com.hongyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.hongyi.common.framework.domain.JsonResult;
import com.hongyi.common.framework.validate.AddGroup;
import com.hongyi.common.framework.validate.EditGroup;
import com.hongyi.common.log.annotation.Log;
import com.hongyi.common.log.enums.BusinessType;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.hongyi.common.web.controller.BaseController;
import com.hongyi.system.domain.SysDictData;
import com.hongyi.system.domain.dto.SysDictDataDto;
import com.hongyi.system.domain.vo.SysDictDataVo;
import com.hongyi.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author DingHao
 */
@ApiSort(value = 6)
@Api(tags = "数据字典信息接口")
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {

    /**
     * 服务对象
     */
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 字典类型分页列表
     *
     * @param sysDictDataDto 字典对象
     * @param pageQuery 分页对象
     * @return
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "字典类型列表")
    @SaCheckPermission("system:dict:list")
    @GetMapping(value = "/list")
    public PageData<SysDictDataVo> list(SysDictDataDto sysDictDataDto, PageQuery pageQuery) {
        return sysDictDataService.selectPageDictDataList(sysDictDataDto, pageQuery);
    }

    /**
     * 根据编码查询字典详情
     *
     * @param dictCode 字典编码
     * @return 字典详情信息
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "根据编码查询字典详情")
    @ApiImplicitParam(name = "dictCode", value = "字典编码", paramType = "path", dataTypeClass = Long.class, required = true)
    @SaCheckPermission("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public JsonResult<SysDictDataVo> getInfo(@PathVariable Long dictCode) {
        return JsonResult.ok(sysDictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "根据类型查询字典详情")
    @ApiImplicitParam(name = "dictType", value = "字典类型", paramType = "path", dataTypeClass = String.class, required = true)
    @GetMapping(value = "/type/{dictType}")
    public JsonResult<List<SysDictDataVo>> dictType(@PathVariable String dictType) {
        List<SysDictDataVo> sysDictDataVos = sysDictDataService.selectDictDataByType(dictType);
        return JsonResult.ok(CollectionUtils.isEmpty(sysDictDataVos) ? Collections.emptyList() : sysDictDataVos);
    }

    /**
     * 新增字典类型
     *
     * @param sysDictDataDto 字典对象
     * @return
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "新增字典类型")
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public JsonResult<Void> add(@Validated(AddGroup.class) @RequestBody SysDictDataDto sysDictDataDto) {
        sysDictDataService.insertDictData(sysDictDataDto);
        return JsonResult.ok();
    }

    /**
     * 修改保存字典类型
     *
     * @param sysDictDataDto 字典对象
     * @return
     */
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "修改字典类型")
    @SaCheckPermission("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public JsonResult<Void> edit(@Validated(EditGroup.class) @RequestBody SysDictDataDto sysDictDataDto) {
        sysDictDataService.updateDictData(sysDictDataDto);
        return JsonResult.ok();
    }

    /**
     * 删除字典类型
     *
     * @param dictCodes 字典ID数组
     * @return
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除字典类型")
    @ApiImplicitParam(name = "dictCodes", value = "数据字典ID数组", paramType = "path", dataTypeClass = Long.class, allowMultiple = true, required = true)
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public JsonResult<Void> remove(@PathVariable Long[] dictCodes) {
        sysDictDataService.deleteDictDataByIds(dictCodes);
        return JsonResult.ok();
    }

}
